package hongcaosp.app.android.user.dynamic;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import hongcaosp.app.android.R;
import hongcaosp.app.android.comment.CommentFragment;
import hongcaosp.app.android.comment.LoadMoreHandler;
import hongcaosp.app.android.common.BaseAdapter;
import hongcaosp.app.android.common.BaseHolder;
import hongcaosp.app.android.common.PresenterListener;
import hongcaosp.app.android.modle.bean.IUser;
import hongcaosp.app.android.modle.bean.IVideo;
import hongcaosp.app.android.user.FreshInterface;
import hongcaosp.app.android.common.IListView;
import hongcaosp.app.android.modle.bean.IDynamic;
import hongcaosp.app.android.user.TotalCallBack;
import hongcaosp.app.android.user.UserDetailActivity;
import hongcaosp.app.android.video.PlayConstant;
import hongcaosp.app.android.video.ShareUI;
import hongcaosp.app.android.video.VideoEvent;
import hongcaosp.app.android.video.VisiblePage;
import hongcaosp.app.android.video.player.PlayVideoManager;
import hongcaosp.app.android.video.player.VideoPresenter;
import xlj.lib.android.base.component.BaseFragment;
import xlj.lib.android.base.toastcompat.ToastManager;

public class DynamicFragment extends BaseFragment implements VisiblePage, IListView<IDynamic>,FreshInterface, DynamicHolder.VideoClickListener {
    private RecyclerView recyclerView;
    private BaseAdapter<IDynamic> dynamicAdapter;
    private DynamicPlayerManager playerManager;
    private DynamicPresenter dynamicPresenter;
    private TotalCallBack totalCallBack;
    private VideoPresenter videoPresenter;
    private long userId;
    private LoadMoreHandler loadMoreHandler;

    public void setTotalCallBack(TotalCallBack totalCallBack) {
        this.totalCallBack = totalCallBack;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        PlayConstant.isPlayOk = true;
    }

    @Override
    public void initView() {
        recyclerView = findViewById(R.id.recycler_view);
        dynamicAdapter = new BaseAdapter<IDynamic>() {
            @NonNull
            @Override
            public BaseHolder<IDynamic> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                DynamicHolder dynamicHolder = new DynamicHolder(getContext(),
                        LayoutInflater.from(getContext()).inflate(R.layout.item_dynamic,parent,false),
                        DynamicFragment.this);
                dynamicHolder.setVideoClickListener(DynamicFragment.this);
                return dynamicHolder;
            }
        };
        playerManager = new DynamicPlayerManager(recyclerView,getContext());
        recyclerView.setAdapter(dynamicAdapter);


        dynamicPresenter = new DynamicPresenter(this, userId);
        dynamicPresenter.freshData(totalCallBack);

        videoPresenter= new VideoPresenter();

        recyclerView.addOnScrollListener(loadMoreHandler = new LoadMoreHandler((LinearLayoutManager) recyclerView.getLayoutManager()) {
            @Override
            public void loadMore() {
                dynamicPresenter.loadMore();
            }
        });
    }

    @Override
    public int getLayoutRes() {
        return R.layout.view_recycle;
    }

    @Override
    public void freshList(List<IDynamic> data, boolean canLoadMore) {
        dynamicAdapter.setData(data);
        dynamicAdapter.notifyDataSetChanged();
        if (loadMoreHandler != null) {
            loadMoreHandler.loadMoreFinish(canLoadMore);
        }
    }

    @Override
    public void addList(List<IDynamic> data, boolean canLoadMore) {
        dynamicAdapter.addData(data);
        dynamicAdapter.notifyDataSetChanged();
        if (loadMoreHandler != null) {
            loadMoreHandler.loadMoreFinish(canLoadMore);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(VideoEvent videoEvent) {
        if (videoEvent.type == 2) {
            //转发成功
            if (dynamicAdapter.getItemCount() <= 0) return;
            List<IDynamic> data = dynamicAdapter.getData();
            for (int i = 0;i < data.size(); i ++) {
                IVideo v = data.get(i);
                if (v.getVid() == videoEvent.video.getVid()) {
                    v.setForwardCounts(videoEvent.video.getForwardCounts());
                    DynamicHolder vh = (DynamicHolder) recyclerView.findViewHolderForAdapterPosition(i);
                    vh.freshForwords();
                    break;
                }
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        PlayVideoManager.getInstance().destroy(this);
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onHide() {
        super.onHide();
        PlayVideoManager.getInstance().onHide(this);
    }

    @Override
    public void onShow() {
        super.onShow();
        PlayVideoManager.getInstance().onShow(this);
    }

    @Override
    public void fresh() {
        if (dynamicPresenter != null) {
            dynamicPresenter.freshData(totalCallBack);
        }
    }

    @Override
    public void onUserClick(IUser user) {
        UserDetailActivity.toDetail(getContext(),user.getUserId());
    }

    @Override
    public void onShare(IDynamic videoPlayer) {
        ShareUI shareUI = new ShareUI(getContext(),videoPlayer);
        shareUI.show();
    }

    @Override
    public void onLike(IDynamic videoPlayer, final DynamicHolder videoHolder) {
        if(videoPresenter != null) {
            videoPresenter.likeVideo(videoPlayer, new PresenterListener() {
                @Override
                public void onSuccess() {
                    videoHolder.freshLike();
                }

                @Override
                public void onFail(String message) {
                    ToastManager.getInstance().showToast(message);
                }
            });
        }
    }

    @Override
    public void unLike(IDynamic videoPlayer,final DynamicHolder videoHolder) {
        if (videoPresenter != null) {
            videoPresenter.cancelikeVideo(videoPlayer, new PresenterListener() {
                @Override
                public void onSuccess() {
                    videoHolder.freshLike();
                }

                @Override
                public void onFail(String message) {
                    ToastManager.getInstance().showToast(message);
                }
            });
        }
    }

    @Override
    public void onFollow(IDynamic videoPlayer, DynamicHolder videoHolder) {

    }

    @Override
    public void onComment(IDynamic videoPlayer,final DynamicHolder videoHolder) {
        CommentFragment commentFragment = new CommentFragment();
        commentFragment.setVideo(videoPlayer);
        commentFragment.setOnCommentListener(new CommentFragment.OnCommentListener() {
            @Override
            public void onCommentCount() {
                videoHolder.freshComment();
            }
        });
        commentFragment.show(getFragmentManager(),"");

    }
}
